<?php

/**
 * Nemovitosti presenter.
 */
class NemovitostiPresenter extends BasePresenter
{
    public function renderSeznam($id = null, $typ = null){
        $estateModel = new EstateModel($this->db);

        $estateModel->where("deleted = ? AND (commission_status = ? OR commission_status = ?)", 
                                                    array(0, 
                                                            CommissionStatus::REZERVACE, 
                                                            CommissionStatus::AKTIVNI))
                                                ->order("registration_number DESC");

        $title = $estateModel->setType($id);
        $subtitle = $estateModel->setSubtype($typ);

        $this->template->estates = $estateModel;            
        $this->template->mainTitle = $title;
        $this->template->subtitle = $subtitle;        
    }
    
    public function renderImport() {
        if ($this->user->loggedIn) {
            $estateImport = new EstateImport($this->db);
            $this->template->estates = $estateImport->getEstateIdList();
        } else {
            $this->flashMessage("Pro vstup do této sekce musíte být přihlášeni.", "error");
            $this->redirect("Administrace:default");
        }
    }
    
	/*
    public function renderUpdate() {
        $estateImport = new EstateImport($this->db);
        $estateImport->updateAllEstates();
    }
	*/
    
    public function handleImportOne($id) {
        $estateImport = new EstateImport($this->db);
        $estateImport->importEstate($id);
    }
    
    public function renderDetail($id, $nabidka) {
        $estateModel = new EstateModel($this->db);
        $estate = $estateModel->get($id);        
        if (!is_object($estate)) {
            throw new NBadRequestException();
        }
        $this->template->estate = $estate;
        if ($estate !== FALSE || $estate->deleted) {
            $this->template->info = $this->getInfo($estate);
        }
        
        $this->template->agent = $estate->ref("agent", "agent");
        
    }
    
    /**
     * Vytvori pole s informacemi o nabidce v zavislosti na typu nemovitosti.
     * Klice jsou nazvy vlastnosti a hodnoty jsou hodnoty vlastnosti.
     * @param NTableRow $estate
     * @return array 
     */
    private function getInfo($estate) {
        $info = array();
        
        switch($estate->estate_type) {
            case EstateTypes::DUM:
            case EstateTypes::CINZOVNI_DUM:
                $info = array(
                    "lokalita" => $estate->town/* . (!empty($estate->street) ? ", " . $estate->street : "") . (!empty($estate->house_number) ? " " . $estate->house_number : "")*/,
                    "typ nemovitosti" => EstateTypes::$values[$estate->estate_type] . " (" . EstateSubtypes::$values[$estate->estate_subtype] . ")",
                    "stav nabídky" => $this->formatCommissionStatus($estate->commission_status),
                    "dispozice" => Dispositions::$values[$estate->disposition],
                    "typ vlastnictví" => $estate->ownership_type,
                    "užitná plocha" => $estate->area_use . 'm<span class="super">2</span>',
                    "plocha pozemku" => $estate->area_of_lot . 'm<span class="super">2</span>',
                    "konstrukce" => $estate->building_structure,
                    "nadzemních podlaži" => $estate->aboveground_floor,
                    "podzemních podlaží" => $estate->underground_floor,
                    "elektřina" => empty($estate->electricity) ? "nezadáno" : $estate->electricity,
                    "plyn" => empty($estate->gas) ? "nezadáno" : $estate->gas,
                    "stav budovy" => EstateStatus::$values[$estate->estate_status],
                    "ev. číslo" => empty($estate->registration_number) ? 'nezadáno' : $estate->registration_number,
                    "cena" => $this->formatPrice($estate->price, $estate->currency, $estate->price_inc_comm),
                    );
                break;
            case EstateTypes::BYT:
                $info = array(
                    "lokalita" => $estate->town/* . (!empty($estate->street) ? ", " . $estate->street : "") . (!empty($estate->house_number) ? " " . $estate->house_number : "")*/,
                    "typ nemovitosti" => EstateTypes::$values[$estate->estate_type],
                    "stav nabídky" => $this->formatCommissionStatus($estate->commission_status),
                    "dispozice" => Dispositions::$values[$estate->disposition],
                    "patro" => empty($estate->floor) ? "nezadáno" : $estate->floor,                    
                    "užitná plocha" => $estate->floor_area . 'm<span class="super">2</span>',
                    "typ vlastnictví" => $estate->ownership_type,                    
                    "konstrukce" => $estate->building_structure,
                    "stav budovy" => EstateStatus::$values[$estate->estate_status],
                    "ev. číslo" => empty($estate->registration_number) ? 'nezadáno' : $estate->registration_number,
                    "cena" => $this->formatPrice($estate->price, $estate->currency, $estate->price_inc_comm),
                    );
                break;
            case EstateTypes::KOMERCNI:
                $info = array(
                    "lokalita" => $estate->town /*. (!empty($estate->street) ? ", " . $estate->street : "") . (!empty($estate->house_number) ? " " . $estate->house_number : "")*/,
                    "typ nemovitosti" => EstateTypes::$values[$estate->estate_type] . " (" . EstateSubtypes::$values[$estate->estate_subtype] . ")",
                    "stav nabídky" => $this->formatCommissionStatus($estate->commission_status),      
                    "ev. číslo" => empty($estate->registration_number) ? 'nezadáno' : $estate->registration_number,
                    "cena" => $this->formatPrice($estate->price, $estate->currency, $estate->price_inc_comm),
                    );
                break;
            case EstateTypes::OSTATNI:
                $info = array(
                    "lokalita" => $estate->town/* . (!empty($estate->street) ? ", " . $estate->street : "") . (!empty($estate->house_number) ? " " . $estate->house_number : "")*/,
                    "typ nemovitosti" => EstateTypes::$values[$estate->estate_type] . " (" . EstateSubtypes::$values[$estate->estate_subtype] . ")",
                    "stav nabídky" => $this->formatCommissionStatus($estate->commission_status),                    
                    "užitná plocha" => $estate->area_use . 'm<span class="super">2</span>',
                    "plocha pozemku" => $estate->area_of_lot . 'm<span class="super">2</span>',   
                    "ev. číslo" => empty($estate->registration_number) ? 'nezadáno' : $estate->registration_number,
                    "cena" => $this->formatPrice($estate->price, $estate->currency, $estate->price_inc_comm),
                    );
                break;
            case EstateTypes::POZEMEK:
                $info = array(
                    "lokalita" => $estate->town/* . (!empty($estate->street) ? ", " . $estate->street : "") . (!empty($estate->house_number) ? " " . $estate->house_number : "")*/,
                    "typ pozemku" => EstateSubtypes::$values[$estate->estate_subtype],
                    "stav nabídky" => $this->formatCommissionStatus($estate->commission_status),
                    "plocha parcely:" => $estate->area_of_lot . 'm<span class="super">2</span>',
                    "voda:" => empty($estate->water) ? "ne" : $estate->water,
                    "elektřina:" => empty($estate->electricity_on_lot) ? "ne" : $estate->electricity_on_lot,
                    "plyn:" => empty($estate->gas) ? "ne" : $estate->gas,            
                    "ev. číslo" => empty($estate->registration_number) ? 'nezadáno' : $estate->registration_number,
                    "cena" => $this->formatPrice($estate->price, $estate->currency, $estate->price_inc_comm),
                    );
                break;
        }
        
        return $info;
    }
    
    /**
     * Vrati textovou verzi stavu nabidky obalenou spanem s prisluznou tridou.
     * @param int $status
     * @return string 
     */
    private function formatCommissionStatus($status) {
        $class = "";
        
        switch($status) {
            case CommissionStatus::AKTIVNI:
                $class = "commision-active";
                break;
            case CommissionStatus::REZERVACE:
                $class = "commision-reservation";
                break;
            case CommissionStatus::ZREALIZOVANO:
                $class = "commision-realized";
                break;
            default:
                break;
        }
        
        
        if (!empty($class)) {
            $class = " class=$class";
        }
        
        return "<span$class>" . CommissionStatus::$values[$status] . "</span>"; 
    }
    
    /**
     * Vrati cenu v pozadovanem formatu.
     * @param double $price cena
     * @param string $currency mena
     * @param bool $incComm Obashuje cena provizi pro RK
     * @return string 
     */
    private function formatPrice($price, $currency, $incComm) {
        $priceString = number_format($price, 0, ',', ' ') . " " . $currency;
        $priceString = "<strong>$priceString</strong>";
        
        if ($incComm) {
            $priceString .= " (vč. provize RK)";
        } else {
            $priceString .= " (bez provize RK)";
        }
        
        return $priceString;
    }
}
